package com.asman.worker.rn

import android.content.Intent
import android.net.Uri
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import java.util.*

/**
 * author : ybk
 * date   : 2019-07-18 19:47
 * desc   :
 */
class NativeMap(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

    override fun getName(): String {
        return "NativeMap"
    }

    override fun getConstants(): Map<String, Any>? {
        return HashMap()
    }

    /**
     * https://api.map.baidu.com/geocoder?location=30.2770989131,119.9714363918&output=html                       百度经纬度
     * https://api.map.baidu.com/geocoder?location=30.2711924471,119.9648934603&coord_type=gcj02&output=html      高德经纬度
     * baidumap://map/geocoder?location=30.2770989131,119.9714363918                                              百度经纬度
     * baidumap://map/geocoder?location=30.2711924471,119.9648934603&coord_type=gcj02                             高德经纬度
     * 跳转地图
     * @param latitude 纬度
     * @param longitude 经度
     * @param address 详细地址
     * @return null
     */
    @ReactMethod
    fun show(latitude: String?, longitude: String?, address: String?) {
        try {
            var url = ""
            if (latitude != null && longitude != null) {
                url = "baidumap://map/geocoder?location=$latitude,$longitude&coord_type=gcj02"
            } else if (address != null) {
                url = "baidumap://map/geocoder?address=$address"
            }
            val intent = Intent()
            intent.data = Uri.parse(url)
            currentActivity?.startActivity(intent)
        } catch (e: Exception) {
            var url = ""
            if (latitude != null && longitude != null) {
                url = "https://api.map.baidu.com/geocoder?location=$latitude,$longitude&coord_type=gcj02&output=html"
            } else if (address != null) {
                url = "https://api.map.baidu.com/geocoder?address=$address&output=html"
            }
            val intent1 = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            currentActivity?.startActivity(intent1)
        }
    }
}
